import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'landing',
      component: () => import('@/views/LandingView.vue'),
      meta: {
        title: '博客首页'
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
      meta: { 
        guest: true // 已登录用户不应访问此页面
      }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/RegisterView.vue'),
      meta: { 
        guest: true // 已登录用户不应访问此页面
      }
    },
    {
      path: '/blogs',
      name: 'blogs',
      component: () => import('../views/BlogsView.vue')
    },
    {
      path: '/blogs/:id',
      name: 'blog-detail',
      component: () => import('@/views/BlogDetailView.vue'),
      meta: {
        title: '博客详情'
      }
    },
    {
      path: '/user',
      name: 'user',
      component: () => import('../views/user/LayoutView.vue'),
      meta: { requiresAuth: true },
      children: [
        {
          path: '',
          redirect: { name: 'dashboard' }
        },
        {
          path: 'dashboard',
          name: 'dashboard',
          component: () => import('../views/user/DashboardView.vue')
        },
        {
          path: 'profile',
          name: 'profile',
          component: () => import('../views/user/ProfileView.vue')
        },
        {
          path: 'my-blogs',
          name: 'my-blogs',
          component: () => import('../views/user/MyBlogsView.vue')
        },
        {
          path: 'create-blog',
          name: 'create-blog',
          component: () => import('../views/user/CreateBlogView.vue')
        },
        {
          path: 'edit-blog/:id',
          name: 'edit-blog',
          component: () => import('../views/user/EditBlogView.vue')
        },
        {
          path: 'blog-types',
          name: 'blog-types',
          component: () => import('../views/user/BlogTypeManageView.vue')
        },
        {
          path: 'blogs',
          name: 'BlogManage',
          component: () => import('@/views/user/BlogManageView.vue'),
          meta: {
            requiresAuth: true,
            title: '博客管理'
          }
        }
      ]
    },
    {
      path: '/admin',
      name: 'admin',
      component: () => import('../views/admin/LayoutView.vue'),
      meta: { requiresAdmin: true },
      children: [
        {
          path: '',
          redirect: { name: 'admin-dashboard' }
        },
        {
          path: 'dashboard',
          name: 'admin-dashboard',
          component: () => import('../views/admin/DashboardView.vue')
        },
        // {
        //   path: 'users',
        //   name: 'admin-users',
        //   component: () => import('../views/admin/UsersView.vue')
        // },
        // {
        //   path: 'blogs',
        //   name: 'admin-blogs',
        //   component: () => import('../views/admin/BlogsView.vue')
        // },
        // {
        //   path: 'blog-types',
        //   name: 'admin-blog-types',
        //   component: () => import('../views/admin/BlogTypesView.vue')
        // },
        // {
        //   path: 'comments',
        //   name: 'admin-comments',
        //   component: () => import('../views/admin/CommentsView.vue')
        // },
        // {
        //   path: 'logs',
        //   name: 'admin-logs',
        //   component: () => import('../views/admin/LogsView.vue')
        // }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      component: () => import('../views/NotFoundView.vue')
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 安全地从localStorage获取数据
  let token = null
  let isAdmin = false
  
  try {
    token = localStorage.getItem('token')
    isAdmin = localStorage.getItem('isAdmin') === 'true'
  } catch (error) {
    console.error('Failed to get authentication data from localStorage', error)
    // 发生错误时清除数据并重新登录
    localStorage.removeItem('token')
    localStorage.removeItem('user')
    localStorage.removeItem('isAdmin')
  }
  
  // 需要登录但未登录的路由
  if (to.meta.requiresAuth && !token) {
    next({ name: 'login', query: { redirect: to.fullPath } })
  } 
  // 需要管理员权限但不是管理员的路由
  else if (to.meta.requiresAdmin && (!token || !isAdmin)) {
    next({ name: 'login', query: { redirect: to.fullPath } })
  }
  // 已登录用户访问登录/注册页面时重定向
  else if (to.meta.guest && token) {
    // 管理员重定向到管理控制台，普通用户重定向到用户中心
    next(isAdmin ? { name: 'admin-dashboard' } : { name: 'dashboard' })
  }
  else {
    next()
  }
})

export default router 